using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

using FreeFlow.Administration;
using FreeFlow.WinFormsControls.HelperClasses;

namespace FreeFlowAdministrator
{
	/// <summary>
	/// Summary description for AttachmentsControl.
	/// </summary>
	public class AttachmentsControl : System.Windows.Forms.UserControl
	{
    private ExtendedDataGrid attachmentsGrid;
    private IContainer components;

		public AttachmentsControl()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

      actionList = new UIActionList();
      actionList.Add("Save selected attachment", "Save the selected attachment", 
        new ActionExecuteDelegate(SaveAttachment));
      actionList.Add("Open selected attachments", "Open the selected attachments", 
        new ActionExecuteDelegate(OpenAttachment));
      actionList.Add("Delete selected attachments", "Delete the selected attachments from the database", 
        new ActionExecuteDelegate(DeleteAttachment));
      actionList.Add("Copy to clipboard", "Copy the list of attachments to the clipboard", 
        new ActionExecuteDelegate(CopyToClipboard));
		}

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
      this.components = new System.ComponentModel.Container();
      this.attachmentsGrid = new FreeFlowAdministrator.ExtendedDataGrid();
      ((System.ComponentModel.ISupportInitialize)(this.attachmentsGrid)).BeginInit();
      this.SuspendLayout();
      // 
      // attachmentsGrid
      // 
      this.attachmentsGrid.AllowNavigation = false;
      this.attachmentsGrid.CaptionVisible = false;
      this.attachmentsGrid.DataMember = "";
      this.attachmentsGrid.Dock = System.Windows.Forms.DockStyle.Fill;
      this.attachmentsGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
      this.attachmentsGrid.Location = new System.Drawing.Point(0, 0);
      this.attachmentsGrid.Name = "attachmentsGrid";
      this.attachmentsGrid.Size = new System.Drawing.Size(552, 344);
      this.attachmentsGrid.TabIndex = 2;
      this.attachmentsGrid.OpenSelected += new System.EventHandler(this.attachmentsGrid_OpenSelected);
      // 
      // AttachmentsControl
      // 
      this.Controls.Add(this.attachmentsGrid);
      this.Name = "AttachmentsControl";
      this.Size = new System.Drawing.Size(552, 344);
      ((System.ComponentModel.ISupportInitialize)(this.attachmentsGrid)).EndInit();
      this.ResumeLayout(false);

    }
		#endregion

    private UIActionList actionList;
    public UIActionList ActionList
    {
      get
      {
        return actionList;
      }
    }

    private string DeleteAttachment()
    {
      if (MessageBox.Show("Delete the selected attachments?", "Confirm", MessageBoxButtons.YesNo) == DialogResult.Yes)
      {
        attachmentsGrid.DeleteSelected();
      }
      return null;
    }

    private string OpenAttachment()
    {
      using (new CursorHandler())
      {
        CurrencyManager bm = 
          (CurrencyManager)attachmentsGrid.BindingContext[attachmentsGrid.DataSource];
        for (int i = 0; i < bm.Count; i++) 
        { 
          if (attachmentsGrid.IsSelected(i))
          {
            Attachment attachment = (Attachment)bm.List[i];
            string fileName = Path.GetTempPath() + attachment.FileName;
            attachment.SaveToFile(fileName);
            Process.Start(fileName);
          }
        }
      }
      return null;
    }

    private string SaveAttachment()
    {
      if (attachmentsGrid.CurrentRowIndex > -1)
      {
        BindingManagerBase bm = attachmentsGrid.BindingContext[attachmentsGrid.DataSource];
        Attachment attachment = (Attachment)bm.Current;
        using (SaveFileDialog dialog = new SaveFileDialog())
        {
          dialog.FileName = attachment.FileName;
          if (dialog.ShowDialog() == DialogResult.OK)
          {
            attachment.SaveToFile(dialog.FileName);
          }
        }
      }
      return null;
    }

    public void LoadAttachments(AttachmentCollection attachments)
    {
      attachmentsGrid.DataSource = null;
      attachmentsGrid.DataSource = attachments;
      // remove server column
      attachmentsGrid.RemoveColumn("Server");
      attachmentsGrid.SetCaption("FileName", "File name");
      attachmentsGrid.AutosizeColumns();
    }

    private string CopyToClipboard()
    {
      attachmentsGrid.CopyToClipboard();
      return null;
    }

    private void attachmentsGrid_OpenSelected(object sender, EventArgs e)
    {
      OpenAttachment();
    }
	}
}
